/* 配置错误处理 */

/*
 例子:
 {
     errcode: 'NO LOGIN',                         //错误代码
     prompt: '你还没有登录或登录超时,请登录...',      //错误提示,空字符为不提示
     url: '/login'                               //跳转地址,true为刷新当前页,空字符串为不跳转
     fun: function () {                          //操作方法,空字符串为无

     }
 }
 */

let is_init = true;
let array = [],
    num = 0;

function next(dataArr, type) {
    for (let i = 0; i < dataArr.length; i++) {
        type === 'ajax' ? tools.ajax(dataArr[i]) : null;
        num++;
    }
    if (num === dataArr.length) {
        is_init = true;
        array = [];
        num = 0;
    }
}

export default {
    data: [{
        errcode: 'NO LOGIN',
        prompt: '',
        url: '',
        fun: function (res) {
            array.push(res.option);
            if (is_init) {
                unit.alert.message('你还没有登录或登录超时,请登录...');
                window.unit.local.del('token');
                if (window.location.pathname.indexOf('/root_login') > -1) {
                    unit.router.replace({
                        path: '/login',
                        query: {}
                    });
                } else {
                    tools.router.replace({
                        path: '/login',
                    });
                }
            }


            setTimeout(() => {
                next(array);
            }, 1000);

            is_init = false;

        }
    }]

}